package com.jy.wms.common.generator;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @ClassName: Vmgenerator
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author zzp
 * @Date 2017/8/24 9:26
 */
public class Vmgenerator {
    MysqlConnection mc=new MysqlConnection();
    GenEntityMysql gem=new GenEntityMysql();
    String javaController="";
    String javaFileName="";
    public Vmgenerator() {

        createController();//生成完controller
        System.out.println("生成完controller");
        createService();
        System.out.println("生成完Service");
        createMapper();

        System.out.println("生成完Mapper");
        createMapperXml();
        System.out.println("生成完Xml");

    }
    public void createMapperXml(){
        Properties prop=mc.getProperties("/createBean.properties");
        String templatePath="wms\\src\\main\\resources\\templates\\templateMapper.xml.vm";
        String outFileName=prop.getProperty("javaFileName")+"Mapper.xml";
        Map<String,Object> map=new HashMap<String ,Object>();
        GeneratorBean gb=new GeneratorBean();
        gb.setBasePackagePath(gem.packageOutDao);
        gb.setClassName(outFileName);
        gb.setMappingPath(prop.getProperty("javaFileName").toLowerCase());
        gb.setModelName(prop.getProperty("javaFileName"));
        gb.setAuthorName(gem.authorName);
        gb.setDateTime(new Date());
        gb.setColnames(gem.colnames);
        gb.setColTypes(gem.colTypes);
        gb.setTableName(gem.tablename);
        gb.setPrimaryKey(gem.tableKey);
        map.put("bean",gb);
        createVm(templatePath,outFileName,map,gem.mapperxmlbasepath,gem.packageOutMapper);
    }
    public void createMapper(){
        Properties prop=mc.getProperties("/createBean.properties");
        String templatePath="wms\\src\\main\\resources\\templates\\templateMapper.vm";
        String outFileName=prop.getProperty("javaFileName")+"Mapper.java";
        Map<String,Object> map=new HashMap<String ,Object>();
        GeneratorBean gb=new GeneratorBean();
        gb.setBasePackagePath(gem.packageOutDao);
        gb.setClassName(outFileName);
        gb.setMappingPath(prop.getProperty("javaFileName").toLowerCase());
        gb.setModelName(prop.getProperty("javaFileName"));
        gb.setAuthorName(gem.authorName);
        gb.setDateTime(new Date());
        map.put("bean",gb);
        createVm(templatePath,outFileName,map,gem.mapperbasepath,gem.packageOutDao);
    }
    public void createService(){
        Properties prop=mc.getProperties("/createBean.properties");
        String templatePath="wms\\src\\main\\resources\\templates\\templateService.vm";
        String outFileName=prop.getProperty("javaFileName")+"Service.java";
        String outFilePath="";
        Map<String,Object> map=new HashMap<String ,Object>();
        GeneratorBean gb=new GeneratorBean();
        gb.setBasePackagePath(gem.packageOutServiceImpl);
        gb.setClassName(outFileName);
        gb.setMappingPath(prop.getProperty("javaFileName").toLowerCase());
        gb.setModelName(prop.getProperty("javaFileName"));
        gb.setAuthorName(gem.authorName);
        gb.setDateTime(new Date());
        map.put("bean",gb);
        createVm(templatePath,outFileName,map,gem.servicebasepath,gem.packageOutServiceImpl);
    }

    public void createController(){
        Properties prop=mc.getProperties("/createBean.properties");
        javaController=prop.getProperty("javaController");
        String templatePath="wms\\src\\main\\resources\\templates\\templateController.vm";
        String outFileName=javaController+".java";
        String outFilePath="";
        Map<String,Object> map=new HashMap<String ,Object>();
        GeneratorBean gb=new GeneratorBean();
        gb.setBasePackagePath(gem.packageOutController);
        gb.setClassName(javaController);
        gb.setMappingPath(prop.getProperty("javaFileName").toLowerCase());
        gb.setModelName(prop.getProperty("javaFileName"));
        gb.setAuthorName(gem.authorName);
        gb.setPrimaryKey(initcap(gem.tableKey));
        gb.setDateTime(new Date());
        map.put("bean",gb);
        createVm(templatePath,outFileName,map,gem.controllerBasePath,gem.packageOutController);
    }

    public void createVm(String templatePath, String outFileName,Map<String,Object> map,String basePath,String packagePath){
        VelocityEngine ve = new VelocityEngine();
        //得到模板文件
        System.out.println(templatePath);
        String baseTemplatePath="";
        Template template = ve.getTemplate(templatePath, "UTF-8");
        VelocityContext context = new VelocityContext();
        //传入参数
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            context.put(entry.getKey(),entry.getValue());
        }
            /*context.put("javaFileName", "User");
        context.put("bean", new User());*/

        try {
            //生成xml
            FileWriter fileWriter = getFileWriter(outFileName,basePath,packagePath);
            //调用merge方法传入context
            template.merge(context, fileWriter);
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static FileWriter getFileWriter(String fileName,String baseFilePath,String outFilePath) throws IOException {
        File directory = new File("");
        String outputPath = directory.getAbsolutePath()+ "/"+baseFilePath.replace(".", "/")+"/"+outFilePath.replace(".", "/")+"/"+initcap(fileName) + "";
        File outputFile = new File(outputPath);
        return new FileWriter(outputFile);
    }
    /**
     * 功能：将输入字符串的首字母改成大写
     * @param str
     * @return
     */
    private static String initcap(String str) {

        char[] ch = str.toCharArray();
        if(ch[0] >= 'a' && ch[0] <= 'z'){
            ch[0] = (char)(ch[0] - 32);
        }

        return new String(ch);
    }

    public static void main(String[] args) {
        new Vmgenerator();
    }
}
